<?php

/**
 * This is the model class for table "events".
 *
 * The followings are the available columns in table 'events':
 * @property integer $id
 * @property string $main_title
 * @property string $sub_title
 * @property string $description
 * @property string $location
 * @property string $address
 * @property string $contact
 * @property string $start_date
 * @property string $start_time
 * @property string $slug
 * @property string $tag
 * @property string $map
 * @property integer $category_id
 * @property integer $gallery_id
 * @property string $event_code
 * @property integer $priority
 * @property integer $language_id
 * @property string $created_at
 * @property integer $created_by
 * @property string $updated_at
 * @property integer $updated_by
  * @property integer $active_status
  *@property string $num_view
 *
 * The followings are the available model relations:
 * @property Galleries $gallery
 * @property EventCategories $category
 * @property Languages $language
 * @property Admins $createdBy
 * @property Admins $updatedBy
 * @property Tickets[] $tickets
 */
class Event extends CActiveRecord
{
	public $time ;
	public $fileImage;
	public $tmpGallery;
	public $images;
	public $image_slide;

    /**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Event the static model class
	 */
    public static function model($className=__CLASS__)
    {
    	return parent::model($className);
    }

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'events';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category_id,gallery_id,event_code,start_date, start_time', 'required'),
//			array('category_id,event_code,start_date, start_time', 'required'),
			array('category_id, gallery_id, priority, created_by, updated_by', 'numerical', 'integerOnly'=>true),
			array('event_code', 'length', 'max'=>255),
			array('video_link,status, start_date, start_time, slug, tag, map, created_at, updated_at', 'safe'),
			array('fileImage', 'file', 'on' => 'create,update',
				'allowEmpty' => true,
				'types' => 'jpg,gif,png',
				'wrongType' => Yii::t('lang', 'Only jpg,gif,png are allowed.'),
                           'maxSize' => MyFunctionCustom::maxFileUpload(), // 5MB
                           'tooLarge' => Yii::t('lang', 'The file was larger than 500 KB.'),
                           ),




			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, main_title, sub_title,video_link, sub_title1, sub_title2,status, description, location, address, contact, start_date, start_time, slug, tag, map, category_id, gallery_id, event_code, priority, language_id, cover_image, created_at, created_by, updated_at, updated_by,time,tmpGallery, active_status,num_view', 'safe', 'on'=>'search'),
			);
}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'gallery' => array(self::BELONGS_TO, 'Gallery', 'gallery_id'),
			'category' => array(self::BELONGS_TO, 'EventCategory', 'category_id'),
			// 'language' => array(self::BELONGS_TO, 'Language', 'language_id'),
			'createdBy' => array(self::BELONGS_TO, 'Admin', 'created_by'),
			'updatedBy' => array(self::BELONGS_TO, 'Admin', 'updated_by'),
			'tickets' => array(self::HAS_MANY, 'Ticket', 'event_id'),
			'translate' => array(self::HAS_MANY, 'Translate', 'event_id'),
			);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'main_title' => 'Main Title',
			'sub_title' => 'Sub Title',
			'sub_title1' => 'Sub Title 1',
			'sub_title2' => 'Sub Title 2',
			'video_link' => 'Video Link',
			'description' => 'Description',
			'location' => 'Location',
			'address' => 'Address',
			'contact' => 'Contact',
			'start_date' => 'Start Date',
			'start_time' => 'Start Time',
			'slug' => 'Slug',
			'tag' => 'Tag',
			'map' => 'Map',
			'category_id' => 'Category',
			'gallery_id' => 'Gallery',
			'event_code' => 'Event Code',
			'priority' => 'Priority',
			'status' => 'Status',
			'language_id' => 'Language',
			'ranking_default' => 'Ticket type',
			// 'hot' => 'Hot',
			'cover_image' => 'Cover Image',
			'created_at' => 'Created At',
			'created_by' => 'Created By',
			'updated_at' => 'Updated At',
			'updated_by' => 'Updated By',
			'fileImage'=>'Seat Map'
			);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		// $criteria->compare('main_title',$this->main_title,true);
		// $criteria->compare('sub_title',$this->sub_title,true);
		// $criteria->compare('description',$this->description,true);
		// $criteria->compare('location',$this->location,true);
		// $criteria->compare('address',$this->address,true);
		$criteria->compare('contact',$this->contact,true);
		$criteria->compare('start_date',$this->start_date,true);
		$criteria->compare('start_time',$this->start_time,true);
		$criteria->compare('slug',$this->slug,true);
		$criteria->compare('tag',$this->tag,true);
		$criteria->compare('map',$this->map,true);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('gallery_id',$this->gallery_id);
		$criteria->compare('event_code',$this->event_code,true);
		$criteria->compare('priority',$this->priority);
		// $criteria->compare('language_id',$this->language_id);
		// $criteria->compare('hot',$this->hot);
		$criteria->compare('cover_image',$this->cover_image);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('created_by',$this->created_by);
		$criteria->compare('updated_at',$this->updated_at,true);
		$criteria->compare('updated_by',$this->updated_by);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			));
	}


	public function getAll($expired = null) {

		$criteria = new CDbCriteria;
		$criteria->with = 'translate';
		$criteria->order = 't.id DESC';
		if($expired != null){
            $criteria->compare('t.status', $expired);
        }
		$result = self::model()->findAll($criteria);
		if ($result)
			return $result;
		return array();
	}
	public function getEventActive($status = NULL) {

		$criteria = new CDbCriteria;
		$criteria->with = 'translate';
		$criteria->order = 't.id DESC';
		if($status != NULL){
            $criteria->compare('t.status','<>'. STATUS_EXPIRED);
        }
		$result = self::model()->findAll($criteria);
		if ($result)
			return $result;
		return array();
	}

	protected function beforeSave() {
		$this->start_date = date('Y-m-d h:i:s',  strtotime($this->start_date));

		return parent::beforeSave();
	}

    /**
     * <Jason>
     * <Email: pmhai90@gmail.com>
     * @return string
     */
}